﻿using System;
using System.Windows;
using System.Windows.Controls;
using YwangHRMSys.DAL;
using YwangHRMSys.Model;


namespace YwangHRMSys.UI.SystemMgr.ContractMgr
    {
        /// <summary>
        ///     OldContractWindow.xaml 的交互逻辑
        /// </summary>
        public partial class OldContractWindowEdit : Window
            {

                public OldContractWindowEdit()
                {
                    InitializeComponent();
                }


                public Guid EditGuid { get; set; }

                private void LoadData()
                {
                    ContractInfoModel model = new ContractInfoDAL().GetByContractInfoID(EditGuid);
                    BlockSignedTime.Text = model.ContractSigned.Year.ToString("D");
                    BlockExpiresTime.Text = model.ContractExpires.Year.ToString("D");
                    TxtContractSigned.Text = model.ContractSigned.Date.ToString("yyyy-MM-dd");
                    TxtContractExpires.Text = model.ContractExpires.Date.ToString("yyyy-MM-dd");
                    TxtOldRemarks.Text = model.ContractRemarks;
                    EmployeeInfoModel employeeInfoModel = new EmployeeInfoDAL().GetById
                            (
                                    model.EmployeeNameId);
                    LabelName.Content = employeeInfoModel.Name + "合同续签信息表";
                    EmplyeeName.Content = employeeInfoModel.Name;

                }


                private void Window_Loaded( object sender, RoutedEventArgs e )
                {
                    LoadData();
                }

                private void BtnSave_Click( object sender, RoutedEventArgs e )
                {

                    if (MessageBox.Show
                                (
                                        "确定要对\"" + EmplyeeName.Content + "\"进行合同续签吗！\n此过程是不可逆的！",
                                        "提示",
                                        MessageBoxButton.YesNo,
                                        MessageBoxImage.Question) == MessageBoxResult.Yes)
                        {
                            var model = new ContractInfoModel();
                            if (DateExpiresTime.SelectedDate != null)
                                {
                                    var expiresTime = (DateTime) DateExpiresTime.SelectedDate;
                                    if (DateSignedTime.SelectedDate != null)
                                        {
                                            var signedTime = (DateTime) DateSignedTime.SelectedDate;

                                            model.ContractSigned = signedTime;
                                            model.ContractExpires = expiresTime;
                                            model.EmployeeExpires = expiresTime.Date.ToString("yyyy-MM-dd");
                                            model.EmployeeSiged = signedTime.Date.ToString("yyyy-MM-dd");
                                            model.ContractLength = TxtLength.Text;
                                            model.ContractRemarks = TxtRemarks.Text;
                                            model.Id = EditGuid;
                                        }
                                    ContractInfoModel now = new ContractInfoDAL().GetByContractInfoID(EditGuid);

                                    OldContractInfoModel old =new OldContractInfoModel();

                                    old.Id = Guid.NewGuid();
                                    old.EmployeeId = now.EmployeeNameId;
                                    old.OldContractLength = now.ContractLength;
                                    old.OldContractSigned = now.EmployeeSiged;
                                    old.OldContractExpires = now.EmployeeExpires;
                                    old.OldContractRemarks = now.ContractRemarks;
                                    old.Years =  now.ContractExpires.Year.ToString();
                                

                                    new OldContractInfoDAL().InsertOldContractInfo(old);
                                    new ContractInfoDAL().UpdateIsRenewed(model);
                               
                                }

                            else
                                {
                                    MessageBox.Show("请填入合同时间");
                                }
                            MessageBox.Show("信息续签成功");

                            DialogResult = true;
                        }


                }

                #region 时间联动

                private void DateSignedTime_SelectedDateChanged( object sender, SelectionChangedEventArgs e )
                {
                    if (DateSignedTime.SelectedDate != null)
                        {
                            var signedTime = (DateTime) DateSignedTime.SelectedDate;
                            DateExpiresTime.SelectedDate = signedTime.AddYears(1);
                        }
                    else
                        {
                            MessageBox.Show("信息不能为空");
                        }

               

                }

                private void DateExpiresTime_SelectedDateChanged( object sender, SelectionChangedEventArgs e )
                {

                    if (DateExpiresTime.SelectedDate != null)
                        {
                            if (DateSignedTime.SelectedDate != null)
                                {
                                    int years = DateExpiresTime.SelectedDate.Value.Year
                                                - DateSignedTime.SelectedDate.Value.Year;
                                    TxtLength.Text = years + "年";
                                }
                        }
                    else
                        {
                            MessageBox.Show("信息不能为空");
                        }


                }

                #endregion
            }
    }